// pages/index/students/payment/payment.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // classDetail:{},
      teachers:{},
      userInfo: {},
      hasUserInfo: false,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),

    gameChoice: '',
    shichang: '',
    levelCode: '',
    platform: '',
    shichang:'',
    paycoupon:'',
    price:''

  },

  bindtap:function(e){
    console.log(this.data.teachers,'e')
    var teacherId = app.globalData.teacherid;
    var classHour=app.globalData.shichang;
    var level = app.globalData.levelCode;
    var totalPrice = this.data.teachers.price;
    var courseType = app.globalData.courseType;
    var openid = app.globalData.openid;
    var couponId;
    console.log(openid,'openid'); 
    console.log(teacherId, 'teacherId');
    console.log(classHour, 'classHour');
    console.log(level, 'level');
    console.log(totalPrice, 'totalPrice');
    console.log(courseType, 'courseType');
    console.log(app.globalData.couponId,'app.globalData.couponId');
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/addOrder',
      method: 'POST',
      data: {
        studentOpenid:openid,
        teacherId: teacherId,
        classHour: classHour,
        level: level,
        totalPrice: totalPrice,
        courseType: courseType,
        couponId: app.globalData.couponId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res.data,'reres.data.datas');
        if(res.data.code==200){
          // wx.showToast({
          //   title:'下单成功',
          //   duration: 10000,
          //   icon: "success"
          // })

          setTimeout(() => {
            wx.showToast({
              title: '下单成功',
              icon: "success",
            });
            setTimeout(() => {
              wx.hideToast();
            }, 2000)
          }, 0);
         
          wx.switchTab({
            url: '../../../../../homeTab/homeTab',
          })
        } else if (res.data.code == 500){
          // wx.showToast({
          //   title: "下单失败，余额不足，请充值",
          // })
          // wx.navigateTo({
          //   url: '/pages/profileTab/mywallet/mywallet',
          // })

          console.log(res,'支付res')
          wx.showModal({
            title: '前往充值',
            content: '下单失败，余额不足，请充值',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateTo({
                  url: '/pages/profileTab/mywallet/mywallet',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
       
      },
      fail: res => {
       
      },
    });
  
  },
  card:function(){
    // wx.request({
    //   url: 'https://www.enjoyyz.com:8443/yinzuo/coupon',
    //   method:'GET',
    //   data: {
    //     userId: app.globalData.userid,
    //   },
    //   success:function(res){
    //     console.log(res);
    //     console.log(app.globalData.userid,'app.globalData.userid 1');
    //   }
    // })
    wx.navigateTo({
      url: '/pages/profileTab/coupon/coupon',
      success: function(res) { 
        app.globalData.paycoupon=1;
        console.log(app.globalData.paycoupon,'app.globalData.paycoupon')
      },
      fail: function(res) {},
      complete: function(res) {},
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // teacherName: '',
    // teacherId: '',
    // tercherPic: '',
    // price: '',
    var gameChoice;
    var shichang;
    var levelCode;
    var platform;
    var shichang;
    var price;
    // var teachers = app.globalData.searchResult;
    const url = 'https://www.enjoyyz.com:8443/yinzuo/start';
    var teachers = app.globalData.searchResult;
    console.log(teachers,'teachers');
    console.log(app.globalData.platform,'app.globalData.platform 支付开始')
    console.log(app.globalData.gameChoice,'app.globalData.gameChoice')
    console.log(app.globalData.levelCode_code,'app.globalData.levelCode_code')
    console.log(app.globalData.system,'app.globalData.system')
    console.log(app.globalData.courseType,'app.globalData.courseType')
    wx.request({
      url: url,
      data:{
        //  platform: app.globalData.platform,
        // gameChoice: app.globalData.gameChoice,
        levelCode: app.globalData.levelCode_code,
        //  system: app.globalData.system,
        courseType: app.globalData.courseType,
        duration: app.globalData.shichang1,
        userId: app.globalData.userid
      },
      success: res => {
        console.log(res,'res')
        // teachers = app.globalData.searchResult;
        // console.log(app.globalData.searchResult, 'aaaaa')
        if (app.globalData.couponId1 == 1) {
          app.globalData.searchResult.price = 0;
          app.globalData.couponId1 = 0;
        }
        this.setData({
          teachers: teachers,
          teachers: app.globalData.searchResult, 
          price: app.globalData.searchResult.price,
          platform: app.globalData.platform,
          gameChoice: app.globalData.gameChoice,
          levelCode: app.globalData.levelCode,
          system: app.globalData.system,
          courseType: app.globalData.courseType,
          shichang: app.globalData.shichang1
          
        })
        
        console.log(app.globalData.searchResult, 'app.globalData.searchResult')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 授权登录按钮获取用户信息
    getUserInfo: function (e) {
    var that = this
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
})